/* Questa parte è relativa al salvataggio dei dati in sicurezza e al caricamento di quest'ultimi in sicurezza*/

package gestoremulte.pack;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import android.os.Environment;
import android.util.Log;

public class EncryptionDecription {

	private static String LocationFileVerbale = Environment.getExternalStorageDirectory()+File.separator+"GestioneMulte"+File.separator+"Files"+File.separator+"Verbali"+File.separator;
	private static String LocationFileNotifica = Environment.getExternalStorageDirectory()+File.separator+"GestioneMulte"+File.separator+"Files"+File.separator+"Notifiche"+File.separator;
	private static String LocationFileACIPRA = Environment.getExternalStorageDirectory()+File.separator+"GestioneMulte"+File.separator+"Files"+File.separator+"ACIPRA"+File.separator;
	private static String LocationFileAutosave = Environment.getExternalStorageDirectory()+File.separator+"GestioneMulte"+File.separator+"Files"+ File.separator+"temp"+File.separator;
	EncryptionDecription(){
		
	}
	
	public void encryptSaveVerbale(Verbale v,boolean autosave) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
		FileOutputStream fos = null;
		if (!autosave)
			fos = new FileOutputStream(LocationFileVerbale+v.getNumeroVerbale());
		
		else
			fos = new FileOutputStream(LocationFileAutosave+"tempVerbale");
	    // Length is 16 byte
	    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
	    // Create cipher
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.ENCRYPT_MODE, sks);
	    // Wrap the output stream
	    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
	    // Write bytes

	    ByteArrayOutputStream bos   = new ByteArrayOutputStream();
	    ObjectOutputStream oos      = new ObjectOutputStream(bos);
	    oos.writeObject(v);
	    oos.flush();
	    oos.close();
	    
	    byte[] data = bos.toByteArray();

	    bos.close();
	    
	    cos.write(data);

	    cos.flush();
	    cos.close();
	    
	}
	
	public Verbale decryptLoadVerbale(String el,boolean autosave)throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ClassNotFoundException {
	    
		File file = null;
		FileInputStream fis = null;
		
		if (!autosave){
			file = new File(LocationFileVerbale+el);
			fis = new FileInputStream(LocationFileVerbale+el);
		}
		else{
			file = new File(LocationFileAutosave+"tempVerbale");	
			fis = new FileInputStream(LocationFileAutosave+"tempVerbale");
		}
	 
	    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.DECRYPT_MODE, sks);
	    CipherInputStream cis = new CipherInputStream(fis, cipher);
	    
	    byte[] d = new byte[(int)file.length()];
	    cis.read(d);
	        
	    ByteArrayInputStream in = new ByteArrayInputStream(d);
	    ObjectInputStream is = new ObjectInputStream(in);
	    Verbale v = (Verbale)is.readObject();

	    Log.e("number", v.getNumeroVerbale());
	    
	    cis.close();
	    is.close();
	    fis.close();

		return v;
	}
	
	public void encryptSaveNotifica(Notifica n, boolean autosave) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
	    
		FileOutputStream fos = null;
		if (!autosave)
			fos = new FileOutputStream(LocationFileNotifica+n.getVerbaleNumber());
		else
			fos = new FileOutputStream(LocationFileAutosave+"tempNotifica");
	    // Length is 16 byte
	    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
	    // Create cipher
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.ENCRYPT_MODE, sks);
	    // Wrap the output stream
	    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
	    // Write bytes

	    ByteArrayOutputStream bos   = new ByteArrayOutputStream();
	    ObjectOutputStream oos      = new ObjectOutputStream(bos);
	    oos.writeObject(n);
	    oos.flush();
	    oos.close();
	    
	    byte[] data = bos.toByteArray();

	    bos.close();
	    
	    cos.write(data);

	    cos.flush();
	    cos.close();
	    
	}
	
	public Notifica decryptLoadNotifica(String el,boolean autosave)throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ClassNotFoundException {
		File file = null;
		FileInputStream fis = null;
		if (!autosave){
			file = new File(LocationFileNotifica+el);
	
			fis = new FileInputStream(LocationFileNotifica+el);

		}
		else{
			file = new File(LocationFileAutosave+"tempNotifica");
			
			fis = new FileInputStream(LocationFileAutosave+"tempNotifica");
		}
	    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.DECRYPT_MODE, sks);
	    CipherInputStream cis = new CipherInputStream(fis, cipher);
	    
	    byte[] d = new byte[(int)file.length()];
	    cis.read(d);
	        
	    ByteArrayInputStream in = new ByteArrayInputStream(d);
	    ObjectInputStream is = new ObjectInputStream(in);
	    Notifica n = (Notifica)is.readObject();

	    Log.e("number", n.getVerbaleNumber());
	    
	    cis.close();
	    is.close();
	    fis.close();

		return n;
	}

	public void deleteNotifica(String el){
		
		File file = new File(LocationFileNotifica+el);
		file.delete();
	}
	
	public void deleteVerbale(String el){
		
		File file = new File(LocationFileVerbale+el);
		file.delete();
	}
	
	public void encryptSaveACIPRA(RispostaACIPRA acipra, String targa) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
		
		FileOutputStream fos = new FileOutputStream(LocationFileACIPRA+targa);

	    // Length is 16 byte
	    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
	    // Create cipher
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.ENCRYPT_MODE, sks);
	    // Wrap the output stream
	    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
	    // Write bytes

	    ByteArrayOutputStream bos   = new ByteArrayOutputStream();
	    ObjectOutputStream oos      = new ObjectOutputStream(bos);
	    oos.writeObject(acipra);
	    oos.flush();
	    oos.close();
	    
	    byte[] data = bos.toByteArray();

	    bos.close();
	    
	    cos.write(data);

	    cos.flush();
	    cos.close();
	}
	
	public RispostaACIPRA decryptLoadACIPRA(String targa) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ClassNotFoundException{
		
		File file = new File(LocationFileACIPRA+targa);

		FileInputStream fis = new FileInputStream(LocationFileACIPRA+targa);

	 
	    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
	    Cipher cipher = Cipher.getInstance("AES");
	    cipher.init(Cipher.DECRYPT_MODE, sks);
	    CipherInputStream cis = new CipherInputStream(fis, cipher);
	    
	    byte[] d = new byte[(int)file.length()];
	    cis.read(d);
	        
	    ByteArrayInputStream in = new ByteArrayInputStream(d);
	    ObjectInputStream is = new ObjectInputStream(in);
	    RispostaACIPRA n = (RispostaACIPRA)is.readObject();

	    cis.close();
	    is.close();
	    fis.close();

		return n;
	}
}
